from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.shortcuts import render,HttpResponse,redirect
from .models import *
from django.db.models import Q
from hashlib import md5


# Create your views here.
def index(request):
    print(request.session.get('role'))
    print(request.session.get('uname'))

    if not request.session.get('role'):
        return HttpResponse('首页')



def add_leaver(request):
    if request.session.get('role') != 1:
        response = HttpResponse('没有权限，拒绝访问',status=403,charset='utf-8')
        response['content_type'] = 'text/html'
        return response

    if request.method == 'GET':
        types =Leavetype.objects.all()
        return render(request,'add_leaver.html',{'types':types})
    else:
        stuname = request.POST.get('stuname','')
        context = request.POST.get('context','')
        createTime = request.POST.get('createTime','')
        tid = request.POST.get('tid','')
        tid = int(tid)
        Leavemod.objects.create(
            stuname=stuname,
            context=context,
            createTime=createTime,
            tid_id=tid,
        )
        return HttpResponse('插入成功')

def show_leaver(request):
    if request.session.get('role') != 2:
        response = HttpResponse('没有权限，拒绝访问',status=403,charset='utf-8')
        response['content_type'] = 'text/html'
        return response
    leave_type = Leavetype.objects.all()
    status = request.GET.get('status','-1')
    # 如果status == -1 查询所有
    # 如果status 没有 查询所有
    # 其他情况 查询所有类型
    if status == '-1':
        leaves = Leavemod.objects.all()
    else:
        leaves = Leavemod.objects.filter(tid_id=int(status)).all()
    status = int(status)
    page = request.GET.get('page',1)
    pageint = int(page)
    pagesize = 2   #每页展示几天数据
    paginator = Paginator(leaves,pagesize)
    pagex = paginator.page(pageint)  # 获取第x页的数据对象
    leaves = pagex.object_list  # 是一个queryset对象


    return render(request,'leaves.html',{'leave_type':leave_type,"leaves":leaves,'page':pageint,'status':status})

def update_leaver(request):
    id = request.GET.get('id')
    status = request.GET.get('status')
    leave =Leavemod.objects.get(id=int(id))
    leave.status = int(status)
    leave.save()
    return redirect('leave:show_leaver')


   #加密
def register(request):
    if request.method == 'GET':
        return render(request,'register.html')
    else:
        uname = request.POST.get('uname','')
        upwd = request.POST.get('upwd','')
        upwd2 = request.POST.get('upwd2','')
        role = request.POST.get('role','')

        if upwd == upwd2:   #存数据，返回登录界面
            try:
                User.objects.get(uname=uname)
                return render(request, 'register.html', {'error': '用户名重复，请换一个'})
            except:
                upwd = md5(upwd.encode(encoding='UTF-8')).hexdigest()   #将明文数据该问密文数据
                User.objects.create(
                    uname=uname,
                    upwd=upwd,
                    role=role
                )
                return HttpResponse('注册成功')
        else:
            return render(request,'register.html',{'error':'两次密码不一样'})



def loginx(request):
    if request.method == 'GET':
        return render(request,'login.html')
    else:
        uname = request.POST.get('uname')
        upwd = request.POST.get('upwd')
        upwd = md5(upwd.encode(encoding='UTF-8')).hexdigest()
        user = User.objects.filter(Q(uname=uname) & Q(upwd=upwd)).last()
        if user:
            request.session['uname'] = user.uname
            request.session['role'] = user.role

            if user.role == 1:
                return redirect("leave:add_leaver")
            else:
                return redirect("leave:show_leaver")

        else:
            return redirect("leave:loginx")















